from __future__ import print_function
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.font_manager import FontProperties
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler    #引入归一化的包
from sklearn import linear_model
import pandas as pd
font = FontProperties(fname=r"C:\windows\fonts\simsun.ttc", size=14)    # 解决windows环境下画图汉字乱码问题




if __name__ == "__main__":
    data_url = "http://lib.stat.cmu.edu/datasets/boston"
    raw_df = pd.read_csv(data_url, sep="\s+", skiprows=22, header=None)
    data = np.hstack([raw_df.values[::2, :], raw_df.values[1::2, :2]])
    target = raw_df.values[1::2, 2]  # 读取数据


    print(data.shape, data.shape[1])
    X, X_test, y, y_test = train_test_split(data, target, test_size=0.2, random_state=42)

    scaler = StandardScaler()
    scaler.fit(X)
    X = scaler.transform(X)
    X_test = scaler.transform(X_test)
    # 线性模型拟合
    model = linear_model.LinearRegression()
    model.fit(X, y)

    # 预测结果
    result = model.predict(X_test)
    print(model.coef_)  # Coefficient of the features 决策函数中的特征系数
    print(model.intercept_)  # 又名bias偏置,若设置为False，则为0
    print(result)  # 预测结果

